﻿var core = (function () {
    function initIndex() {
        try {
            com._cInitPage();
            $_company = $('#_company');
            $_company.datagrid({
                title: '',
                nowrap: false,
                striped: true,
                collapsible: false,
                singleSelect: true,
                remoteSort: false,
                idField: 'Guid',
                onLoadSuccess: function () {
                },
                columns: [[
                    { title: 'Guid', field: 'Guid', hidden: true }
				], [{ field: 'Name', title: '单位名称', width: 150, align: 'center' },
					{ field: 'OUType', title: '单位类型', width: 150, align: 'center' },
                    { field: 'MasterType', title: '主类型', width: 150, align: 'center' },
                    { field: 'SubType', title: '子类型', width: 150, align: 'center'}]],
                pagination: false,
                rownumbers: true,
                toolbar: [{
                    id: 'btnAdd',
                    text: '添加',
                    iconCls: 'icon-add',
                    handler: function () {
                        toolkits.Window.open("_show2", { iconCls: "icon-add", title: "添加单位", width: 550, height: 210, collapsed: false }, "company.edit.html");
                    }
                }, '-', {
                    id: 'btnEdit',
                    text: '修改',
                    iconCls: 'icon-edit',
                    handler: function () {
                        var selected = $('#_company').datagrid('getSelected');
                        if (selected == null) {
                            $.messager.alert('', '请先选择要修改的单位!');
                            return false;
                        }
                        $('#_show').window({ iconCls: "icon-edit", title: "修改单位" });
                        $('#_show').panel({ onLoad: function () {

                        }, href: "1.html"
                        });
                        $('#_show').window("open");
                        $('#_show').panel("refresh", "1.html");
                    }
                }, '-', {
                    id: 'btnDel',
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        var selected = $('#_company ').datagrid('getSelected');
                        if (selected == null) {
                            $.messager.alert('', '请先选择要删除的部门!');
                            return false;
                        }

                        $guid = selected.Guid;
                        $xmlDel = WebHelper.fun.getXmlforJQ(ConstantHelper.Template.Del);
                        $xmlDel.find("Document").attr("DataGuid", $guid).attr("DataType", "Company");
                        ServiceHelper.InitParameters(null, ConstantHelper.Page.UserID, ConstantHelper.Page.TaskOrganizationInfoGuid, $guid, "Company", $xmlDel[0].xml);
                        $result = ServiceHelper.SetData();
                        if ($result == "0") {
                            $.messager.show({
                                title: '信息提示',
                                msg: "删除成功!",
                                showType: 'show'
                            });
                        }
                        else {
                            $.messager.show({
                                title: '信息提示',
                                msg: "删除失败!请联系系统管理员!",
                                showType: 'show'
                            });
                        }
                    }
                }]
            });

            var _al = service.EasyGetData("companylist", false);
            var _$al = com._cGetXmlObj(_al);
            $list = _$al.find("OUDataInfo");
            $json = { total: $list.length, rows: [] };
            $list.each(function (i, n) {
                $json.rows.push({ Guid: $(n).attr("GUID"), Name: $(n).attr("NAME"), OUType: $(n).attr("OUType"), MasterType: $(n).parent().attr("MasterType"), SubType: $(n).parent().attr("SubType") });
            });
            $_company.datagrid("loadData", $json);

            $('#_show').window({
                width: 600,
                height: 260,
                title: '',
                draggable: false,
                modal: true,
                closed: true
            });
        } catch (e) {
            com._cLog("initIndex", e.description, "error");
        }
    }

    function _bindDepartmentGrid(id) {
        ServiceHelper.InitParameters(null, ConstantHelper.Page.UserID, ConstantHelper.Page.TaskOrganizationInfoGuid, id, ConstantHelper.Page.BranchList, null);
        $xmlDoc = ServiceHelper.GetData();
        $result = WebHelper.fun.getXmlforJQ($xmlDoc);
        $list = $result.find("OUDataInfo");
        $json = { total: $list.length, rows: [] };
        $list.each(function (i, n) {
            $row = { Guid: $(n).attr("GUID"), Name: $(n).attr("NAME"), OUType: $(n).attr("OUType") };
            $json.rows.push($row);
        });
        $_department.datagrid("loadData", $json);
    }

    function _getCompanyList() {
        ServiceHelper.InitParameters(null, ConstantHelper.Page.UserID, ConstantHelper.Page.TaskOrganizationInfoGuid, ConstantHelper.Page.CompanyListDataGuid, ConstantHelper.Page.CompanyList, null);
        $xmlDoc = ServiceHelper.GetData();
        $result = WebHelper.fun.getXmlforJQ($xmlDoc);
        $list = $result.find("OUDataInfo");
        $listData = [];
        $list.each(function (i, n) {
            $listData.push({ "id": $(n).attr("GUID"), "text": $(n).attr("NAME") });
        });
        return $listData;
    }

    function _getGroupList() {
        ServiceHelper.InitParameters(null, ConstantHelper.Page.UserID, ConstantHelper.Page.TaskOrganizationInfoGuid, "277b393f-6559-4f79-aa2b-7602d88f8849", "GroupList", null);
        $xmlDoc = ServiceHelper.GetData();
        $result = WebHelper.fun.getXmlforJQ($xmlDoc);
        $list = $result.find("OUDataInfo");
        $listData = [];
        $list.each(function (i, n) {
            $listData.push({ "id": $(n).attr("GUID"), "text": $(n).attr("NAME") });
        });
        return $listData;
    }

    function _getBranchList(id) {
        ServiceHelper.InitParameters(null, ConstantHelper.Page.UserID, ConstantHelper.Page.TaskOrganizationInfoGuid, id, ConstantHelper.Page.BranchList, null);
        $xmlDoc = ServiceHelper.GetData();
        $result = WebHelper.fun.getXmlforJQ($xmlDoc);
        $list = $result.find("OUDataInfo");
        $lists = [];
        $list.each(function (i, n) {
            $lists.push({ "id": $(n).attr("GUID"), "text": $(n).attr("NAME") });
        });
        return $lists;
    }

    function _getMemberList(id) {
        ServiceHelper.InitParameters(null, ConstantHelper.Page.UserID, ConstantHelper.Page.TaskOrganizationInfoGuid, id, ConstantHelper.Page.BranchMemberList, null);
        $xmlDoc = ServiceHelper.GetData();
        $result = WebHelper.fun.getXmlforJQ($xmlDoc);
        $list = $result.find("OUDataInfo");
        $mlist = [];
        $list.each(function (i, n) {
            $mlist.push({ "id": $(n).attr("GUID"), "text": $(n).attr("NAME") });
        });
        return $mlist;
    }

    function _getApplicationList() {
        ServiceHelper.InitParameters(null, ConstantHelper.Page.UserID, ConstantHelper.Page.TaskOrganizationInfoGuid, ConstantHelper.Page.ApplicationListDataGuid, ConstantHelper.Page.ApplicationList, null);
        $xmlDoc = ServiceHelper.GetData();
        $result = WebHelper.fun.getXmlforJQ($xmlDoc);
        $list = $result.find("OUDataInfo");
        $mlist = [];
        $list.each(function (i, n) {
            $mlist.push({ "id": $(n).attr("GUID"), "text": $(n).attr("NAME") });
        });
        return $mlist;
    }

    return {
        Init: function () {
            initIndex();
        }
    }
})()